import {message} from 'antd';
import {formatMessage} from './intl';

export const newOrUpdateOrderValidator = data => {
    const requiredFields = [];
    if(!data || !data.brandId)
        requiredFields.push(
            formatMessage({
                id: 'validator.brandId',
                defaultMessage: '品牌'
            })
        );    
    if(!data || !data.sourceCurrency)
        requiredFields.push(
            formatMessage({
                id: 'validator.sourceCurrency',
                defaultMessage: '本位币'
            })
        );
    if(!data || !data.targetCurrency)
        requiredFields.push(
            formatMessage({
                id: 'validator.targetCurrency',
                defaultMessage: '目标币种'
            })
        );
    if(!data || !data.rate)
        requiredFields.push(
            formatMessage({
                id: 'validator.rate',
                defaultMessage: '汇率'
            })
        );
    if(!data || !data.beginDate)
        requiredFields.push(
            formatMessage({
                id: 'validator.beginDate',
                defaultMessage: '生效日期'
            })
        );
    if(!data || !data.endDate)
        requiredFields.push(
            formatMessage({
                id: 'validator.endDate',
                defaultMessage: '失效日期'
            })
        );
    if(requiredFields.length > 0) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.requied',
            defaultMessage: '必填'
        })}`);
        return false;
    }
    if(data.sourceCurrency === data.targetCurrency) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.contact',
            defaultMessage: '本位币与目标币种不能相同!'
        })}`);
        return false;
    }
    if(data.endDate < data.beginDate) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.notbefore',
            defaultMessage: '失效日期不可在生效日期之前!'
        })}`);
        return false;
    }
    return true;
};
